package model

import (
	"mini-sql/utils"
)

type TableTuple struct {
	RowID  int64
	Fields []TableField
}

func (t *TableTuple) GetValueByName(fieldName string) *TableField {
	index := t.GetValueIndexByName(fieldName)
	if index >= 0 {
		copy := t.Fields[index]
		return &copy
	}
	return nil
}

func (t *TableTuple) GetValueIndexByName(fieldName string) int {
	for index, field := range t.Fields {
		if field.Name == fieldName {
			return index
		}
	}
	return -1
}

type TableField struct {
	Name  string
	Value interface{}
}

func (t TableTuple) String() string {
	row := make(map[string]interface{})
	for _, item := range t.Fields {
		row[item.Name] = item.Value
	}
	row[TableRowIdName] = t.RowID
	return utils.FormatJSON(row)
}
